package cn.admin.configcenter.registry.common.util;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class SingletonRepository {

	private static final ConcurrentMap<Object,Object> shard = new ConcurrentHashMap<>(65536);

	public static <T> T getSingleton(T obj) {
		Object previous = shard.putIfAbsent(obj,obj);
		return previous == null ? obj : (T) previous;
	}

	public static int size() {
		return shard.size();
	}

	public static Iterator<Object> readOnlyIterator() {
		return ReadonlyIterator.wrap(shard.keySet().iterator());
	}

	public static void remove(Object obj) {
		shard.remove(obj);
	}

}
